﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Web;


namespace IMark.DAO
{
    public class SanPhamModels
    {
        public static List<SAN_PHAM> GetAll()
        {
            using (var context = new EMartEntities())
            {
                return context.SAN_PHAM.Include("LOAI_SAN_PHAM").Where(u=>u.Xoa == 0).ToList();                
            }
        }
        public static SAN_PHAM GetByID(long ID)
        {
            using(var context = new EMartEntities())
            {
                return context.SAN_PHAM.Find(ID);
            }
        }
        public static bool UpdateStatus(int ID, bool Status)
        {
            using (var context = new EMartEntities())
            {
                try
                {
                    SAN_PHAM existing = context.SAN_PHAM.Find(ID);
                    if (Status == true)
                        existing.TrangThai = 0;
                    else
                        existing.TrangThai = 1;
                    context.SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }
        public static bool UpdateSpectial(int ID, bool Spectial)
        {
            using (var context = new EMartEntities())
            {
                try
                {
                    SAN_PHAM existing = context.SAN_PHAM.Find(ID);
                    if (Spectial == true)
                        existing.DacBiet = 0;
                    else
                        existing.TrangThai = 1;
                    context.SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }
        public static bool Update(SAN_PHAM sanPham)
        {
            using (var context = new EMartEntities())
            {
                try
                {
                    SAN_PHAM existing = context.SAN_PHAM.Find(sanPham.ID);
                    existing.DacBiet = sanPham.DacBiet;
                    existing.DonGia = sanPham.DonGia;
                    existing.GiamGia = sanPham.GiamGia;
                    existing.IDLoaiSanPham = sanPham.IDLoaiSanPham;
                    existing.MetaDescription = sanPham.MetaDescription;
                    existing.MetaKeyword = sanPham.MetaKeyword;
                    existing.MetaTitle = sanPham.MetaTitle;
                    existing.MoTaChiTiet = sanPham.MoTaChiTiet;
                    existing.MoTaTomTat = sanPham.MoTaTomTat;
                    existing.NgayBatDau = sanPham.NgayBatDau;
                    existing.NgayKetThuc = sanPham.NgayKetThuc;
                    existing.NgaySua = sanPham.NgaySua;
                    existing.SoLuong = sanPham.SoLuong;
                    existing.TenSanPham = sanPham.TenSanPham;
                    existing.ThongTinThem = sanPham.ThongTinThem;
                    existing.ThuongHieu = sanPham.ThuongHieu;
                    existing.TrangThai = sanPham.TrangThai;
                    context.SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }
        public static bool UpdatePromotion(SAN_PHAM sanPham)
        {
            using (var context = new EMartEntities())
            {
                try
                {
                    SAN_PHAM existing = context.SAN_PHAM.Find(sanPham.ID);
                    existing.GiamGia = sanPham.GiamGia;                    
                    existing.NgayBatDau = sanPham.NgayBatDau;
                    existing.NgayKetThuc = sanPham.NgayKetThuc;
                    existing.NgaySua = sanPham.NgaySua;                                        
                    context.SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }
        public static bool DeleteAllProductType(long IDLoaiSanPham)
        {
            using (var context = new EMartEntities())
            {
                List<SAN_PHAM> lstProduct = context.SAN_PHAM.Where(u => u.IDLoaiSanPham == IDLoaiSanPham).ToList();
                if(lstProduct != null && lstProduct.Count >0)
                {
                    try
                    {
                        foreach (var item in lstProduct)
                        {
                            context.SAN_PHAM.Remove(item);
                        }
                        context.SaveChanges();
                        return true;
                    }
                    catch (Exception)
                    {
                        return false;
                        throw;
                    }                    
                }
                else
                {
                    return true;
                }
            }
        }
        public static bool Delete(long ID)
        {
            using(var context = new EMartEntities())
            {
                try
                {
                    SAN_PHAM existing = context.SAN_PHAM.Find(ID);
                    existing.Xoa = 1;
                    context.SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                    throw;
                }
            }
        }
        public static List<SAN_PHAM> LaySanPhamTheoLoai(long ID,List<SAN_PHAM> ltSanPham)
        {
            EMartEntities context = null;
            try
            {
                context = new EMartEntities();
                List<SAN_PHAM> SanPhamTheoLoai = context.SAN_PHAM.Include("HINH_SAN_PHAM").Include("DANH_GIA").Where(w => w.IDLoaiSanPham == ID && w.TrangThai==1).ToList();
                foreach (SAN_PHAM item in SanPhamTheoLoai)
                {
                    ltSanPham.Add(item);
                }
                List<LOAI_SAN_PHAM> ListLoaiSanPham = context.LOAI_SAN_PHAM.Where(w => w.MaLoaiCha == ID).ToList();
                if (ListLoaiSanPham.Count > 0)
                {
                    foreach (var item in ListLoaiSanPham)
                    {
                        LaySanPhamTheoLoai(item.ID, ltSanPham);
                    }
                }
                return ltSanPham;
            }
            catch(Exception)
            {
                return null;
            }
            finally
            {
                if (context != null)
                    context.Dispose();
            }
        }

        public static SAN_PHAM LaySanPhamTheoID(long ID)
        {
            EMartEntities context = null;
            try
            {
                context = new EMartEntities();
                return context.SAN_PHAM.Include("HINH_SAN_PHAM").Include("DANH_GIA").Single(w => w.ID == ID && w.TrangThai == 1);
                
            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                if (context != null)
                    context.Dispose();
            }
        }  
        public static List<SAN_PHAM> LaySanPhamMoiNhat()
        {
            EMartEntities context = null;
            try
            {
                context = new EMartEntities();
                List<SAN_PHAM> SanPhamTheoLoai = context.SAN_PHAM.Include("HINH_SAN_PHAM").Include("DANH_GIA").Where(w=> w.TrangThai==1).OrderByDescending(o => o.NgayThem).Take(5).ToList();
                return SanPhamTheoLoai;
            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                if (context != null)
                    context.Dispose();
            }
        }

        public static List<SAN_PHAM> LaySanPhamLienQuan(long ID)
        {
            EMartEntities context = null;
            try
            {
                context = new EMartEntities();
                long IDLoai = context.SAN_PHAM.Single(s => s.ID == ID).IDLoaiSanPham;
                return context.SAN_PHAM.Include("HINH_SAN_PHAM").Include("DANH_GIA").Where(w => w.IDLoaiSanPham == IDLoai && w.TrangThai == 1).Take(12).ToList();
            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                if (context != null)
                    context.Dispose();
            }
        }
        public static List<SAN_PHAM> LaySanPhamDacBiet()
        {
            EMartEntities context = null;
            try
            {
                context = new EMartEntities();

                return context.SAN_PHAM.Include("HINH_SAN_PHAM").Include("DANH_GIA").Where(w => w.DacBiet == 1 && w.TrangThai == 1).Take(5).ToList();
            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                if (context != null)
                    context.Dispose();
            }
        }
        public static bool DanhGia(string Ten, string Email, string NoiDung,string sosao,string IDSP)
        {
            EMartEntities context = null;
            try
            {
                context = new EMartEntities();
                DANH_GIA gd = new DANH_GIA();
                gd.NoiDung = NoiDung;
                gd.SoSao = int.Parse(sosao);
                gd.Ten = Ten;
                gd.Email = Email;
                gd.IDSanPham = long.Parse(IDSP);
                gd.NgayThem = DateTime.Now;
                gd.NgaySua = DateTime.Now;
                gd.TrangThai = 1;
                gd.TieuDe = "Tiêu đề";
                context.DANH_GIA.Add(gd);
                context.SaveChanges();
                return true;

            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
                if (context != null)
                    context.Dispose();
            }
        }

        public static string GenerateCartInfo(Dictionary<long, SAN_PHAM> Cart)
        {
            StringBuilder str = new StringBuilder();
            
            foreach (KeyValuePair<long, SAN_PHAM> pair in Cart)
            {
                str.Append("<div class='qc-row qc-row-item'>");
                str.Append("<span class='qc-col-qty'>" + pair.Value.SoLuong + "</span> ");
                str.Append("<span class='qc-col-name'>");
                str.Append("<a href='ChiTietSP.aspx?ID=" + pair.Value.ID + "'>" + pair.Value.TenSanPham + "</a>");
                str.Append("</span> ");
                if (pair.Value.NgayBatDau <= DateTime.Now && pair.Value.NgayKetThuc >= DateTime.Now)
                {
                    long ThanhTien = long.Parse(((pair.Value.DonGia - ((pair.Value.DonGia * pair.Value.GiamGia) / 100)) * pair.Value.SoLuong).ToString());
                    str.Append("<span class='qc-col-price'>" + String.Format(CultureInfo.InvariantCulture, "{0:0,0}", ThanhTien) + "</span>");
                }
                else
                {
                    long ThanhTien = pair.Value.DonGia * pair.Value.SoLuong;
                    str.Append("<span class='qc-col-price'>" + String.Format(CultureInfo.InvariantCulture, "{0:0,0}", ThanhTien) + "</span> ");
                }
                str.Append("<span onclick='RemoveSP("+pair.Value.ID+")' class='qc-col-remove'>");
                str.Append("<i class='fa fa-times fa-fw'></i>");
                str.Append("</span>");
                str.Append("</div>");
            }
            return str.ToString();
        }

        public static long TongThanhTienGioHang(Dictionary<long, SAN_PHAM> Cart)
        {
            long TongThanhTien = 0;
            foreach (KeyValuePair<long, SAN_PHAM> pair in Cart)
            {
                if (pair.Value.NgayBatDau <= DateTime.Now && pair.Value.NgayKetThuc >= DateTime.Now)
                {
                    long ThanhTien = long.Parse(((pair.Value.DonGia - ((pair.Value.DonGia * pair.Value.GiamGia) / 100)) * pair.Value.SoLuong).ToString());
                    TongThanhTien += ThanhTien;
                }
                else
                {
                    long ThanhTien = pair.Value.DonGia * pair.Value.SoLuong;
                    TongThanhTien += ThanhTien;
                }
            }
            return TongThanhTien;
        }

        public static long Add(SAN_PHAM sanPham)
        {
            using (var context = new EMartEntities())
            {
                try
                {
                    context.SAN_PHAM.Add(sanPham);
                    context.SaveChanges();
                    return sanPham.ID;
                }
                catch (Exception)
                {
                    return 0;
                }
            }
        }

        public static List<SAN_PHAM> LaySanPhamMuaNhieu()
        {
            EMartEntities context = null;
            try
            {
                context = new EMartEntities();
                List<SAN_PHAM> SanPhamTheoLoai = context.SAN_PHAM.Include("HINH_SAN_PHAM").Include("DANH_GIA").Where(w => w.CHI_TIET_HD.Count > 0)
                    .OrderByDescending(o => o.CHI_TIET_HD.Sum(d=>d.DonGia*d.SoLuong*(1-d.GiamGia))).Take(12).ToList();
                return SanPhamTheoLoai;
            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                if (context != null)
                    context.Dispose();
            }
        }

        public static List<SAN_PHAM> LaySanPhamKhuyenMai()
        {
            EMartEntities context = null;
            try
            {
                context = new EMartEntities();
                List<SAN_PHAM> SanPhamTheoLoai = context.SAN_PHAM.Include("HINH_SAN_PHAM").Include("DANH_GIA")
                    .Where(w => w.GiamGia!=0 && w.NgayBatDau<=DateTime.Now && w.NgayKetThuc >= DateTime.Now)
                    .OrderByDescending(o=>o.NgayBatDau).ToList();
                return SanPhamTheoLoai;
            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                if (context != null)
                    context.Dispose();
            }
        }

        public static List<SAN_PHAM> LaySanPhamYeuThich(long IDKH)
        {
            EMartEntities context = null;
            try
            {
                context = new EMartEntities();
                List<SAN_PHAM_YEU_THICH> ltYT = context.SAN_PHAM_YEU_THICH.Where(w => w.IDKH == IDKH).ToList();
                List<SAN_PHAM> ltSanPham = new List<SAN_PHAM>();
                foreach (var item in ltYT)
                {
                    SAN_PHAM sp = SanPhamModels.LaySanPhamTheoID(item.IDSanPham);
                    ltSanPham.Add(sp);
                }

                return ltSanPham;
            }
            catch (Exception ex)
            {
                return null;
            }
            finally
            {
                if (context != null)
                    context.Dispose();
            }
        }

        public static List<SAN_PHAM> TimKiemTheoTen(string Ten)
        {
            EMartEntities context = null;
            try
            {
                context = new EMartEntities();
                List<SAN_PHAM> ltSanPham = context.SAN_PHAM.Include("HINH_SAN_PHAM").Include("DANH_GIA").Where(w => w.TenSanPham.Contains(Ten))
                    .OrderByDescending(o=>o.NgayThem).ToList();
                return ltSanPham;
            }
            catch (Exception ex)
            {
                return null;
            }
            finally
            {
                if (context != null)
                    context.Dispose();
            }
        }
    }
}